//
//  DataEngine.swift
//  Swift_WeiBo
//
//  Created by dajie on 15/9/6.
//  Copyright (c) 2015年 dajie. All rights reserved.
//

import UIKit
import Alamofire

class DataEngine: NSObject {
   

    struct Static
    {
        static var instance : DataEngine? = nil
        static var onceToken : dispatch_once_t = 0
    }
    
//  MARK: - 类方法供外界访问
    class func shareEngine() -> DataEngine
    {
        dispatch_once(&Static.onceToken){
            Static.instance = DataEngine()
        }
        return Static.instance!
    }
}

extension DataEngine
{
//    var complete : (AnyObject?, )
    /**
    请求AccessTokent
    */
    func getAccessTokentRequest(parmes : [String : AnyObject] , complete : (JSON : AnyObject?,error : NSError?) -> ()){
        
        Alamofire.request(.POST, "https://api.weibo.com/oauth2/access_token", parameters: parmes as [String : AnyObject]).responseJSON { (_, _, Result) -> Void in

            complete(JSON: Result.value, error: nil)
        }
    }
    
    /**
    获取用户的基本信息
    */
    func getUserAccountRequest(parms : [String : AnyObject] ,complete : (JSON : AnyObject?,error : NSError?) -> ()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        Alamofire.request(.GET, urlString, parameters:parms).responseJSON { (_, _, Result) -> Void in
            
            complete(JSON: Result.value, error: nil)
        }
    }
}